In this exercise, we will be using functions from the tidyverse package. You can see we’ve added the chunk option message = FALSE to hide the version information that tidyverse normally displays.

library(tidyverse)
library(gt)

(a) Make a summary table using gt

We’ve provided some code to summarise the icecore data seen in previous exercises. Modify this to present the resulting data frame as a nice table, using the gt() function.

Think about appropriate headings, column names, decimal places.

icecore <- read_csv("icecore.csv")
icecore %>%
  group_by(core) %>%
  summarise(n_samples = n(),
            earliest_year_AD = min(air_age_AD),
            latest_year_AD = max(air_age_AD),
            min_depth_m = min(depth_m),
            max_depth_m = max(depth_m),
            mean_CO2_ppm = mean(CO2_ppm),
            sd_CO2_ppm = sd(CO2_ppm)) %>%
  ungroup() %>%
  gt(caption = md("CO<sub>2</sub> samples from a selection of Antarctic ice cores")) %>%
  fmt_number(n_samples:latest_year_AD, decimals = 0) %>%
  fmt_number(min_depth_m:sd_CO2_ppm, decimals = 1) %>%
  cols_label(
    core = "Core",
    n_samples = "Number of samples",
    earliest_year_AD = "Earliest",
    latest_year_AD = "Latest",
    min_depth_m = "Minimum",
    max_depth_m = "Maximum",
    mean_CO2_ppm = "Mean",
    sd_CO2_ppm = "SD"
  ) %>%
  tab_spanner("Year (AD)", earliest_year_AD:latest_year_AD) %>%
  tab_spanner("Depth (m)", min_depth_m:max_depth_m) %>%
  tab_spanner(md("CO<sub>2</sub> conc. (ppm)"), mean_CO2_ppm:sd_CO2_ppm)
CO2 samples from a selection of Antarctic ice cores
Core Number of samples Year (AD) Depth (m) CO2 conc. (ppm)
Earliest Latest Minimum Maximum Mean SD
DE08 32 1,840 1,969 83.1 228.7 304.6 12.7
DE08-2 11 1,832 1,978 81.1 242.8 319.5 14.8
DSS 41 1,006 1,959 78.0 534.3 288.3 13.2
Vostok 363 −415,152 −334 149.1 3,304.4 232.2 28.5

If you were doing this in a real document, you might want to use the chunk option {r, echo = FALSE, message = FALSE} so that the knitted document displays the resulting table but not the code to produce it.


© 2021 Statistical Consulting Centre, The University of Melbourne.